package com.lc.aiagent.app;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;

import com.lc.aiagent.advisor.MyLoggerAdvisor;
import com.lc.aiagent.chatmemory.FileBasedChatMemory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;

import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.stereotype.Component;

import java.util.List;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * 功能：云图库机器人
 * 作者：lc
 * 日期：2025/5/8 17:31
 */
@Component
@Slf4j
public class PhotoChatClient {

    private final ChatClient chatClient;

    private final String DEFAULT_SYSTEM_PROMPT ="# 角色定义\n" +
            "你是一个具备教育思维的摄影助手PhotoCoach，目标是帮助用户达成他们未明确表达的修图需求。采用苏格拉底式提问法引导用户思考，每次交互需包含至少一个澄清性问题或建议选项。\n" +
            "\n" +
            "# 交互协议\n" +
            "1. 首次对话必须询问核心场景：\n" +
            "   \"您今天主要想解决：\n" +
            "  拍照技巧指C️图片美化｜摄影知识学习\"\n" +
            "\n" +
            "2. 遇到专业术语时自动转换：\n" +
            "   \"『色阶调整』即调节明暗分布，需要我演示具体操作吗？\"\n" +
            "\n" +
            "3. 当用户3分钟内无操作时主动建议：\n" +
            "   \"试试对这张图说『分析曝光问题』或『教我做双重曝光』\"\n" +
            "\n" +
            "# 需求挖掘机制\n" +
            "- 模糊需求触发词表：\n" +
            "  [\"弄好看点\",\"优化一下\",\"专业些\"]\n" +
            "  → 响应时必选触发三级提问流程\n" +
            "  \n" +
            "- 深层需求推理：\n" +
            "  用户频繁调整肤色 → 记忆为偏好人像美颜 → 后续主动推荐\"自动肤色优化\"功能\n" +
            "\n" +
            "# 引导话术库\n" +
            "► 技术引导：\"想更精细控制的话，可以尝试调整__参数\"\n" +
            "► 教育引导：\"这个效果的原理是__，需要详细讲解吗？\"\n" +
            "► 探索引导：\"最近很多用户喜欢__风格，要不要试试？\"";

    record LoveReport(String title, List<String> suggestions) {
    }

    public PhotoChatClient(DashScopeChatModel dashScopeChatModel) {
        // 自定义的ChatMemory实现
        String property = System.getProperty("user.dir")+ "/tem/chatmemory";
        FileBasedChatMemory chatMemory = new FileBasedChatMemory(property);
        // ChatMemory chatMemory = new InMemoryChatMemory();
        this.chatClient = ChatClient.builder(dashScopeChatModel)
                .defaultSystem(DEFAULT_SYSTEM_PROMPT)
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory),
                        new MyLoggerAdvisor())
                .build();
    }

    /**
     * 多轮对话聊天
     * @param prompt 用户输入
     * @param chatId 聊天id
     * @return
     */
    public String doChat(String prompt, String chatId) {
        ChatResponse chatResponse = this.chatClient.prompt()
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10)
                        .param("userid", "123456"))
                .call()
                .chatResponse();

        String restult = chatResponse.getResult().getOutput().getText();
        log.info("result:{}", restult);
        return restult;
    }
    public LoveReport doChatStructured(String prompt, String chatId) {
        LoveReport restult = this.chatClient.prompt()
                .system(DEFAULT_SYSTEM_PROMPT + "每次对话后都要生成拍摄技巧，标题为{技巧名称}的报告，内容为建议列表")
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .call()
                .entity(LoveReport.class);
        log.info("result:{}", restult);
        return restult;
    }
}
